# 作者: spy
# 开发时间: 2021/12/24 11:09
import time

from selenium.webdriver.common.by import By

from base.base_page import BasePage
from pageobject.login_page import LoginPage
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


class TrackMonitor(BasePage):
    """轨迹监控页面"""
    # 页面元素
    trailer_ID_loc = (By.ID, "rc_select_1")  # 输入拖车号
    trailer_first_choice = (By.XPATH, "//input[@aria-activedescendant='rc_select_1_list_0']")  # 拖车选择下拉框的第一项
    monitor_loc = (By.XPATH, "//button[@data-inspector-line='170']")  # 轨迹监控按钮
    location_loc = (By.XPATH, "//button[@data-inspector-line='173']")  # 地点查询按钮
    playback_loc = (By.XPATH, "//button[@data-inspector-line='176']")  # 轨迹回放按钮
    track_choice = (By.ID, "rc_select_2")  # 轨迹选择
    track_first_choice = (By.XPATH, "//input[@aria-activedescendant='rc_select_2_list_0']")  # 轨迹选择下拉框的第一项
    confirm_loc = (By.XPATH, "//button[@class='ant-btn ant-btn-primary']")  # 确定按钮

    # 页面动作
    def track_monitor(self, trailer_ID):  # 轨迹监控
        # 登录
        lp = LoginPage(self.driver)
        lp.login_TTOS("admin", "ant.design")
        # 点击拖车轨迹监控菜单项
        track_monitor_button = self.driver.find_element(By.XPATH,
                                                        "//span[@class='ant-pro-menu-item-title'and text()='拖车轨迹监控']")
        self.driver.execute_script('arguments[0].click();', track_monitor_button)
        # 轨迹监控
        # 等待元素加载完成
        WebDriverWait(self.driver, 30).until(
            EC.visibility_of_element_located((By.ID, "rc_select_1")))
        self.set_keys(TrackMonitor.trailer_ID_loc, trailer_ID)
        self.driver.implicitly_wait(3)
        self.click(TrackMonitor.trailer_first_choice)
        self.driver.implicitly_wait(3)
        self.click(TrackMonitor.monitor_loc)
        self.driver.implicitly_wait(3)

    def location_search(self, trailer_ID):  # 地点查询
        # 登录
        lp = LoginPage(self.driver)
        lp.login_TTOS("admin", "ant.design")
        # 点击拖车轨迹监控菜单项
        track_monitor_button = self.driver.find_element(By.XPATH,
                                                        "//span[@class='ant-pro-menu-item-title'and text()='拖车轨迹监控']")
        self.driver.execute_script('arguments[0].click();', track_monitor_button)
        # 轨迹监控
        # 等待元素加载完成
        WebDriverWait(self.driver, 30).until(
            EC.visibility_of_element_located((By.ID, "rc_select_1")))
        self.set_keys(TrackMonitor.trailer_ID_loc, trailer_ID)
        self.driver.implicitly_wait(3)
        self.click(TrackMonitor.trailer_first_choice)
        self.driver.implicitly_wait(3)
        self.click(TrackMonitor.location_loc)

    def track_playback(self, trailer_ID):  # 轨迹回放
        # 登录
        lp = LoginPage(self.driver)
        lp.login_TTOS("admin", "ant.design")
        # 点击拖车轨迹监控菜单项
        track_monitor_button = self.driver.find_element(By.XPATH,
                                                        "//span[@class='ant-pro-menu-item-title'and text()='拖车轨迹监控']")
        self.driver.execute_script('arguments[0].click();', track_monitor_button)
        # 轨迹监控
        # 等待元素加载完成
        WebDriverWait(self.driver, 30).until(
            EC.visibility_of_element_located((By.ID, "rc_select_1")))
        self.set_keys(TrackMonitor.trailer_ID_loc, trailer_ID)
        # self.click(TrackMonitor.trailer_first_choice)
        self.driver.implicitly_wait(3)
        self.click(TrackMonitor.playback_loc)
        # 点击下拉框的第一项
        self.driver.implicitly_wait(3)
        self.click(TrackMonitor.track_first_choice)
        self.driver.implicitly_wait(3)
        self.click(TrackMonitor.confirm_loc)
        self.driver.implicitly_wait(3)
        # self.click(TrackMonitor.playback_loc)
